package main

import (
	"fmt"
)

type node struct {
	freq int
	value byte
	left *node
	right *node
}

type HuffmanEncoder struct {
	encodingStr string
	huffmanTreeRoot *node
	huffmanEncodingMap map[byte]string
}

func (h *HuffmanEncoder) encode(originString string) string {
	encodeStr := ""
	bytes := []byte(originString)
	for _, char := range bytes {
		encodeStr += h.huffmanEncodingMap[char]
	}
	return encodeStr
}

func (h *HuffmanEncoder) decode(originString string) string {
	decodeStr := ""
	nodePtr := h.huffmanTreeRoot
	bytes := []byte(originString)
	for _, char := range bytes {
		if char == '0' {
			nodePtr = nodePtr.left
		} else {
			nodePtr = nodePtr.right
		}
		if v := nodePtr.value; v != 0 {
			decodeStr += string(v)
			nodePtr = h.huffmanTreeRoot
		}
	}
	return decodeStr
}

func HuffmanEncode(originString string) HuffmanEncoder {
	if originString != "" {
		root := &node{0,0,nil,nil}
		//TODO root's implement
		encodingMap := make(map[byte]string)
		return HuffmanEncoder{encodingStr: originString, huffmanTreeRoot: root, huffmanEncodingMap: encodingMap}
	}
	// TODO ""
}

func main() {
	h := HuffmanEncode("abbbccddddeeeeeefffff")
	es := h.encode("abcdef")
	fmt.Println(es)
	ds := h.decode(es)
	fmt.Println(ds)
}